<template>
  <div class="detail">
    <!-- 商品分类导航 -->
    <TypeNav />

    <!-- 主要内容区域 -->
    <section class="con" v-if="goodsInfo.price">
      <!-- 导航路径区域 -->
      <div class="conPoin">
        <span v-for="item in 4" :key="item">{{
          goodsInfo.categoryView[`category${item}Name`]
        }}</span>
      </div>
      <!-- 主要内容区域 -->
      <div class="mainCon">
        <!-- 左侧放大镜区域 -->
        <div class="previewWrap">
          <!--放大镜效果-->
          <Zoom :imgUrl="goodsInfo.skuInfo.skuDefaultImg" />
          <!-- 小图列表 -->
          <ImageList :imgListUrl="goodsInfo.skuInfo.skuImageList" />
        </div>
        <!-- 右侧选择区域布局 -->
        <div class="InfoWrap">
          <div class="goodsDetail">
            <h3 class="InfoName">
              {{ goodsInfo.skuInfo.skuName }}
            </h3>
            <p class="news">
              推荐选择下方[移动优惠购],手机套餐齐搞定,不用换号,每月还有花费返
            </p>
            <div class="priceArea">
              <div class="priceArea1">
                <div class="title">
                  价&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;格
                </div>
                <div class="price">
                  <i>¥</i>
                  <em> {{ goodsInfo.skuInfo.price }}</em>
                  <span>降价通知</span>
                </div>
                <div class="remark">
                  <i>累计评价</i>
                  <em>65545</em>
                </div>
              </div>
            </div>
            <div class="support">
              <div class="supportArea">
                <div class="title">
                  支&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;持
                </div>
                <div class="fixWidth">
                  以旧换新，闲置手机回收 4G套餐超值抢 礼品购
                </div>
              </div>
              <div class="supportArea">
                <div class="title">配 送 至</div>
                <div class="fixWidth">广东省 深圳市 宝安区</div>
              </div>
            </div>
          </div>

          <div class="choose">
            <div class="chooseArea">
              <div class="choosed"></div>
              <dl v-for="attr1 in goodsInfo.spuSaleAttrList" :key="attr1.id">
                <dt class="title">{{ attr1.saleAttrName }}</dt>
                <dd
                  changepirce="0"
                  v-for="attr2 in attr1.spuSaleAttrValueList"
                  :key="attr2.id"
                  :class="{ active: Number(attr2.isChecked) === 1 }"
                  @click="
                    changeActive(
                      attr1.spuSaleAttrValueList,
                      attr2.saleAttrValueName
                    )
                  "
                >
                  {{ attr2.saleAttrValueName }}
                </dd>
              </dl>
            </div>
            <div class="cartWrap">
              <div class="controls">
                <input
                  autocomplete="off"
                  class="itxt"
                  :value="goodsNum"
                  @change="getNum('input', $event)"
                />
                <a href="javascript:;" class="plus" @click="getNum('add')">+</a>
                <a href="javascript:;" class="mins" @click="getNum('sub')">-</a>
              </div>
              <div class="add">
                <a href="javascript:;" @click="addCart">加入购物车</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  </div>
</template>

<script>
import ImageList from './ImageList/ImageList'
import Zoom from './Zoom/Zoom'
import { reqGoodsDetail, reqAddCart } from '@/api/goodsDetail'
export default {
  name: 'VDetail',
  data() {
    return {
      goodsInfo: {},
      goodsNum: 1,
      goodsSum: sessionStorage.getItem('goodsSum') * 1,
      goodsPriceNum: sessionStorage.getItem('goodsPriceNum') * 1
    }
  },
  components: {
    ImageList,
    Zoom
  },
  methods: {
    // ...mapActions('detail', ['getGoodsDetail'])
    async getGoodsDetail() {
      const res = await reqGoodsDetail(this.$route.params.id)
      // console.log(res)

      if (res.code === 200) {
        this.goodsInfo = res.data
      }
    },
    // 改变选择配置区域的高亮
    changeActive(parentList, son) {
      // console.log(parentList, son)
      parentList.forEach((element) => {
        // 判断点击的属性在属性组中是否有一致的
        // 如果能在属性组中找到，就把这个属性的isChecked改成1
        // 剩余的全部改成0
        element.isChecked = '0'

        if (element.saleAttrValueName === son) {
          element.isChecked = '1'
        }
      })
    },
    // 用户改变购物车数量
    getNum(type, e) {
      if (type === 'input') {
        const { value } = e.target
        // console.log(value)
        const reg = /^([1-9]|[1-9]\d|1\d{2}|200)$/
        const res = reg.test(value)
        if (res) {
          this.goodsNum = res * 1
        } else if (value > 200) {
          // console.log('值大于200')
          this.goodsNum = e.target.value = 200
        } else {
          this.goodsNum = e.target.value = 1
        }
      } else if (type === 'add') {
        if (this.goodsNum === 200) return
        this.goodsNum += 1
      } else if (type === 'sub') {
        if (this.goodsNum === 1) return
        this.goodsNum -= 1
      }
    },
    // 添加购物车
    async addCart() {
      const { goodsNum } = this
      const { id } = this.$route.params
      const res = await reqAddCart(id, this.goodsNum)
      // console.log(res)
      if (res.code === 200) {
        // 获取用户需要加入购物车的详细信息
        const arr = []
        const { skuName, skuDefaultImg } = this.goodsInfo.skuInfo
        const { price } = this.goodsInfo
        // 获取用户选择的配置项
        this.goodsInfo.spuSaleAttrList.forEach((item) => {
          const res = item.spuSaleAttrValueList.find(
            (attr2) => attr2.isChecked === '1'
          )
          const { saleAttrName, saleAttrValueName } = res
          arr.push(`${saleAttrName}:${saleAttrValueName}`)
        })
        // console.log(skuName, skuDefaultImg, price, arr)
        // 把所有收集到的信息，整理成一个对象
        const goodsInfo = { skuName, price, goodsNum, arr, skuDefaultImg }
        // console.log(goodsInfo)
        sessionStorage.setItem('goodsInfo', JSON.stringify(goodsInfo))

        this.goodsSum += goodsNum * 1
        sessionStorage.setItem('goodsSum', this.goodsSum)

        this.goodsPriceNum = Number(price) * this.goodsSum
        sessionStorage.setItem('goodsPriceNum', this.goodsPriceNum)

        alert('添加购物车成功！')
        this.$router.push('/addcartsuccess')
      }
    }
  },
  mounted() {
    // this.getGoodsDetail(this.$route.params.id)
    this.getGoodsDetail()
  }
}
</script>

<style lang="less" scoped>
.detail {
  .con {
    width: 1200px;
    margin: 15px auto 0;

    .conPoin {
      padding: 9px 15px 9px 0;

      & > span + span:before {
        content: '/\00a0';
        padding: 0 5px;
        color: #ccc;
      }
    }

    .mainCon {
      overflow: hidden;
      margin: 5px 0 15px;

      .previewWrap {
        float: left;
        width: 400px;
        position: relative;
      }

      .InfoWrap {
        width: 700px;
        float: right;

        .InfoName {
          font-size: 14px;
          line-height: 21px;
          margin-top: 15px;
        }

        .news {
          color: #e12228;
          margin-top: 15px;
        }

        .priceArea {
          background: #fee9eb;
          padding: 7px;
          margin: 13px 0;

          .priceArea1 {
            overflow: hidden;
            line-height: 28px;
            margin-top: 10px;

            .title {
              float: left;
              margin-right: 15px;
            }

            .price {
              float: left;
              color: #c81623;

              i {
                font-size: 16px;
              }

              em {
                font-size: 24px;
                font-weight: 700;
              }

              span {
                font-size: 12px;
              }
            }

            .remark {
              float: right;
            }
          }

          .priceArea2 {
            overflow: hidden;
            line-height: 28px;
            margin-top: 10px;

            .title {
              margin-right: 15px;
              float: left;
            }

            .fixWidth {
              width: 520px;
              float: left;

              .red-bg {
                background: #c81623;
                color: #fff;
                padding: 3px;
              }

              .t-gray {
                color: #999;
              }
            }
          }
        }

        .support {
          border-bottom: 1px solid #ededed;
          padding-bottom: 5px;

          .supportArea {
            overflow: hidden;
            line-height: 28px;
            margin-top: 10px;

            .title {
              margin-right: 15px;
              float: left;
            }

            .fixWidth {
              width: 520px;
              float: left;
              color: #999;
            }
          }
        }

        .choose {
          .chooseArea {
            overflow: hidden;
            line-height: 28px;
            margin-top: 10px;

            dl {
              overflow: hidden;
              margin: 13px 0;

              dt {
                margin-right: 15px;
                float: left;
              }

              dd {
                float: left;
                margin-right: 5px;
                color: #666;
                line-height: 24px;
                padding: 2px 14px;
                border-top: 1px solid #eee;
                border-right: 1px solid #bbb;
                border-bottom: 1px solid #bbb;
                border-left: 1px solid #eee;

                &.active {
                  color: green;
                  border: 1px solid green;
                }
              }
            }
          }

          .cartWrap {
            .controls {
              width: 48px;
              position: relative;
              float: left;
              margin-right: 15px;

              .itxt {
                width: 38px;
                height: 37px;
                border: 1px solid #ddd;
                color: #555;
                float: left;
                border-right: 0;
                text-align: center;
              }

              .plus,
              .mins {
                width: 15px;
                text-align: center;
                height: 17px;
                line-height: 17px;
                background: #f1f1f1;
                color: #666;
                position: absolute;
                right: -8px;
                border: 1px solid #ccc;
              }

              .mins {
                right: -8px;
                top: 19px;
                border-top: 0;
              }

              .plus {
                right: -8px;
              }
            }

            .add {
              float: left;

              a {
                background-color: #e1251b;
                padding: 0 25px;
                font-size: 16px;
                color: #fff;
                height: 36px;
                line-height: 36px;
                display: block;
              }
            }
          }
        }
      }
    }
  }

  .product-detail {
    width: 1200px;
    margin: 30px auto 0;
    overflow: hidden;

    .aside {
      width: 210px;
      float: left;
      border: 1px solid #ccc;

      .tabWraped {
        height: 40px;

        h4 {
          border-top: 3px solid #fff;
          float: left;
          line-height: 37px;
          width: 105px;
          text-align: center;
          border-bottom: 1px solid #ccc;

          &.active {
            border-top: 3px solid #e1251b;
            border-bottom: 0;
            font-weight: normal;
          }
        }
      }

      .tabContent {
        padding: 10px;

        .tab-pane {
          display: none;

          &.active {
            display: block;
          }

          &:nth-child(1) {
            .partList {
              overflow: hidden;

              li {
                width: 50%;
                float: left;
                border-bottom: 1px dashed #ededed;
                line-height: 28px;
              }
            }

            .goodsList {
              & > li {
                margin: 5px 0 15px;
                border-bottom: 1px solid #ededed;
                padding-bottom: 5px;

                .list-wrap {
                  .p-img {
                    text-align: center;

                    img {
                      width: 152px;
                    }
                  }

                  .price {
                    font-size: 16px;
                    color: #c81623;
                  }

                  .operate {
                    text-align: center;
                    margin: 5px 0;

                    a {
                      background-color: transparent;
                      border: 1px solid #8c8c8c;
                      color: #8c8c8c;
                      display: inline-block;
                      padding: 2px 14px;
                      line-height: 18px;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    .detail {
      width: 980px;
      float: right;

      .fitting {
        border: 1px solid #ddd;
        margin-bottom: 15px;

        .kt {
          border-bottom: 1px solid #ddd;
          background: #f1f1f1;
          color: #333;
          padding: 5px 0 5px 15px;
        }

        .good-suits {
          height: 170px;
          padding-top: 10px;

          .master {
            width: 127px;
            height: 165px;
            text-align: center;
            position: relative;
            float: left;

            img {
              width: 87px;
            }

            p {
              color: #c81623;
              font-size: 16px;
              font-weight: 700;
            }

            i {
              position: absolute;
              top: 48px;
              right: -25px;
              font-size: 16px;
            }
          }

          .suits {
            width: 668px;
            height: 165px;
            float: left;

            .suitsItem {
              float: left;
              width: 127px;
              padding: 0 20px;
              text-align: center;

              img {
                width: 120px;
                height: 130px;
              }

              p {
                font-size: 12px;
              }

              label {
                display: block;
                position: relative;

                input {
                  vertical-align: middle;
                }

                span {
                  vertical-align: middle;
                }
              }
            }
          }

          .result {
            border-left: 1px solid #ddd;
            width: 153px;
            height: 165px;
            padding-left: 20px;
            float: left;

            .num {
              font-size: 14px;
              margin-bottom: 10px;
              margin-top: 10px;
            }

            .price-tit {
              font-weight: bold;
              margin-bottom: 10px;
            }

            .price {
              color: #b1191a;
              font-size: 16px;
              margin-bottom: 10px;
            }

            .addshopcar {
              background-color: #e1251b;
              border: 1px solid #e1251b;
              padding: 10px 25px;
              font-size: 16px;
              color: #fff;
              display: inline-block;
              box-sizing: border-box;
            }
          }
        }
      }

      .intro {
        .tab-wraped {
          background: #ededed;
          // border: 1px solid #ddd;
          overflow: hidden;

          li {
            float: left;

            & + li > a {
              border-left: 1px solid #ddd;
            }

            &.active {
              a {
                // border: 0;
                background: #e1251b;
                color: #fff;
              }
            }

            a {
              display: block;
              height: 40px;
              line-height: 40px;
              padding: 0 11px;
              text-align: center;
              color: #666;
              background: #fcfcfc;
              border-top: 1px solid #ddd;
              border-bottom: 1px solid #ddd;
            }
          }
        }

        .tab-content {
          .tab-pane {
            display: none;

            &.active {
              display: block;
            }

            &:nth-child(1) {
              .goods-intro {
                padding-left: 10px;

                li {
                  margin: 10px 0;
                }
              }

              .intro-detail {
                img {
                  width: 100%;
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>
